package com.zhhs.jzcd.materialtype.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.zhhs.common.utils.DateUtils;
import com.zhhs.common.utils.SecurityUtils;
import com.zhhs.common.utils.StringUtils;
import com.zhhs.framework.web.domain.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhhs.jzcd.materialtype.mapper.BaseMaterialCategoryMapper;
import com.zhhs.jzcd.materialtype.domain.BaseMaterialCategory;
import com.zhhs.jzcd.materialtype.service.IBaseMaterialCategoryService;

/**
 * 素材类型Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class BaseMaterialCategoryServiceImpl implements IBaseMaterialCategoryService
{
    @Autowired
    private BaseMaterialCategoryMapper baseMaterialCategoryMapper;

    /**
     * 查询素材类型
     *
     * @param id 素材类型主键
     * @return 素材类型
     */
    @Override
    public BaseMaterialCategory selectBaseMaterialCategoryById(Long id)
    {
        return baseMaterialCategoryMapper.selectBaseMaterialCategoryById(id);
    }

    /**
     * 查询素材类型列表
     *
     * @param baseMaterialCategory 素材类型
     * @return 素材类型
     */
    @Override
    public List<BaseMaterialCategory> selectBaseMaterialCategoryList(BaseMaterialCategory baseMaterialCategory)
    {
        return baseMaterialCategoryMapper.selectBaseMaterialCategoryList(baseMaterialCategory);
    }

    /**
     * 新增素材类型
     *
     * @param baseMaterialCategory 素材类型
     * @return 结果
     */
    @Override
    public int insertBaseMaterialCategory(BaseMaterialCategory baseMaterialCategory)
    {
        baseMaterialCategory.setCreateBy(SecurityUtils.getUserId().toString());
        baseMaterialCategory.setCreateTime(DateUtils.getNowDate());
        return baseMaterialCategoryMapper.insertBaseMaterialCategory(baseMaterialCategory);
    }

    /**
     * 修改素材类型
     *
     * @param baseMaterialCategory 素材类型
     * @return 结果
     */
    @Override
    public int updateBaseMaterialCategory(BaseMaterialCategory baseMaterialCategory)
    {
        baseMaterialCategory.setUpdateBy(SecurityUtils.getUserId().toString());
        baseMaterialCategory.setUpdateTime(DateUtils.getNowDate());
        return baseMaterialCategoryMapper.updateBaseMaterialCategory(baseMaterialCategory);
    }

    /**
     * 批量删除素材类型
     *
     * @param ids 需要删除的素材类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseMaterialCategoryByIds(Long[] ids)
    {
        return baseMaterialCategoryMapper.deleteBaseMaterialCategoryByIds(ids);
    }

    /**
     * 删除素材类型信息
     *
     * @param id 素材类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseMaterialCategoryById(Long id)
    {
        return baseMaterialCategoryMapper.deleteBaseMaterialCategoryById(id);
    }

    /**
     * 素材树列表
     */
    @Override
    public List<TreeSelect> selectTreeList() {
        List<BaseMaterialCategory> baseMaterialCategories = baseMaterialCategoryMapper.selectBaseMaterialCategoryList(null);
        List<BaseMaterialCategory> trees = buildBaseTree(baseMaterialCategories);
        List<TreeSelect> collect = trees.stream().map(TreeSelect::new).collect(Collectors.toList());
        return collect;
    }

    public List<BaseMaterialCategory> buildBaseTree(List<BaseMaterialCategory> list){
        List<BaseMaterialCategory> returnList = new ArrayList<BaseMaterialCategory>();
        List<Long> tempList = new ArrayList<Long>();
        for (BaseMaterialCategory category : list)
        {
            tempList.add(category.getId());
        }
        for (BaseMaterialCategory category : list)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(list, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = list;
        }
        return returnList;
    }
    private void recursionFn(List<BaseMaterialCategory> list, BaseMaterialCategory t)
    {
        // 得到子节点列表
        List<BaseMaterialCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseMaterialCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<BaseMaterialCategory> getChildList(List<BaseMaterialCategory> list, BaseMaterialCategory t)
    {
        List<BaseMaterialCategory> tlist = new ArrayList<BaseMaterialCategory>();
        Iterator<BaseMaterialCategory> it = list.iterator();
        while (it.hasNext())
        {
            BaseMaterialCategory n = (BaseMaterialCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BaseMaterialCategory> list, BaseMaterialCategory t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
